package mpc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

public class ControlPanel extends JPanel implements ActionListener {
	
	public final static String
	random = "random",
	increasing = "rising",
	decreasing = "decreasing",
	sortStep = "sortStep",
	autostep = "sortToEnd",
	interrupt = "interrupt";
	
	private boolean threadInterrupted = false;
	
	private JButton b1, b2, b3, b4, b5, b6;
	private JLabel label1;
	
	private Runnable r = new tmpForDelay();
	
	private GridBagLayout layout;
	
	ControlPanel(){
		label1 = new JLabel();
		label1.setFont(Font.decode("Arial-BOLD-12"));
		//label1.setHorizontalTextPosition(JLabel.RIGHT);
		label1.setText("Массив заполнен случайными числами.");
		label1.setBorder(BorderFactory.createEtchedBorder());
		
		b1 = new JButton("Случайное заполнение");
		b1.setActionCommand(random);
		b1.addActionListener(this);
		
		b2 = new JButton("Упорядоченный");
		b2.setActionCommand(increasing);
		b2.addActionListener(this);
		
		b3 = new JButton("Обратно упорядоченный");
		b3.setActionCommand(decreasing);
		b3.addActionListener(this);
		
		b4 = new JButton("Сделать шаг сортировки");
		b4.setActionCommand(sortStep);
		b4.addActionListener(this);
		
		b5 = new JButton("Автошаг/Продолжить");
		b5.setActionCommand(autostep);
		b5.addActionListener(this);
		
		b6 = new JButton("Прервать");
		b6.setActionCommand(interrupt);
		b6.addActionListener(this);
		b6.setEnabled(false);
		
		layout = new GridBagLayout();
		this.setLayout(layout);
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		//c.ipadx = 10; c.ipady = 5; c.gridheight = 1;
		c.insets = new Insets(4, 4, 4, 4);
		
		c.gridx = 0; c.gridy = 0; this.add(b1, c);
		c.gridx = 0; c.gridy = 2; this.add(b2, c);
		c.gridx = 0; c.gridy = 4; this.add(b3, c);
		c.gridx = 1; c.gridy = 1; this.add(b4, c);
		//c.gridheight = 2;
		c.gridx = 1; c.gridy = 3; this.add(b5, c);
		c.gridx = 1; c.gridy = 4; this.add(b6, c);
		//JSeparator separ = new JSeparator(SwingConstants.VERTICAL);
		c.fill = GridBagConstraints.BOTH;
		c.gridheight = 5; //c.gridx = 2; c.gridy = 0; this.add(separ); 
		//c.gridwidth = 2;
		//c.fill = GridBagConstraints.NONE;
		//c.anchor = GridBagConstraints.EAST;
		c.insets = new Insets(4, 40, 4, 4);
		c.gridx = 2; c.gridy = 0; this.add(label1, c);
		
		this.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String com = arg0.getActionCommand();
		
		if(com == random){
			System.out.println("ControlPanel: Random called...");
			MainGUI.randomFilling();
			MainGUI.resetSHolders();
			label1.setText("Массив заполнен случайными числами.");
		} else if(com == increasing){
			MainGUI.increasingFilling();
			MainGUI.resetSHolders();
			label1.setText("Массив уже упорядочен.");
		} else if(com == decreasing){
			MainGUI.decreasingFilling();
			MainGUI.resetSHolders();
			label1.setText("Массив упорядочен по убыванию.");
		} else if(com == sortStep){
			MainGUI.doAllNextIteration();
		} else if(com == autostep){
			/*
			while(!MainGUI.allFinished){
				MainGUI.doAllNextIteration();
			}*/
			Thread t = new Thread(r);
			b6.setEnabled(true);
			b5.setEnabled(false);
			b1.setEnabled(false); b2.setEnabled(false);
			b3.setEnabled(false); b4.setEnabled(false);
			t.start();
			label1.setText("Запущены все сортировки.");
		} else if(com == interrupt){
			threadInterrupted = true;
			label1.setText("Все сортировки приостановлены.");
		}
	}
	
	class tmpForDelay implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(!MainGUI.allFinished && !threadInterrupted){
				
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				MainGUI.doAllNextIteration();
			}
			threadInterrupted = false;
			b6.setEnabled(false);
			b5.setEnabled(true);
			b1.setEnabled(true); b2.setEnabled(true);
			b3.setEnabled(true); b4.setEnabled(true);
		}
	}

}
